package com.zis.wechat.controller;

import com.zis.core.SearchParams;
import com.zis.wechat.model.Account;
import com.zis.wechat.service.IAccountInter;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Create by wangshen 2018/1/2
 */
@RestController
@RequestMapping("/api/wechat/account")
public class AccountController {

    @Autowired
    private IAccountInter accountInter;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public PageQuery<Account> findByPage(@RequestBody SearchParams params) throws Exception {
        return accountInter.findByPage(params);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String insert(HttpServletRequest request, @RequestBody Account account) throws Exception {
        account.setCreateUser(request.getAttribute("code").toString());
        return accountInter.insert(account);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(HttpServletRequest request, @RequestBody Account account) throws Exception {
        account.setUpdateUser(request.getAttribute("code").toString());
        return accountInter.update(account);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable("id") String id) throws Exception{
        return accountInter.delete(id);
    }

    @RequestMapping(value = "/set/cache", method = RequestMethod.GET)
    @ResponseBody
    public String setAllToRedis() throws Exception {
        return accountInter.setAllToRedis();
    }
}
